﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using PortableVersion.Core.FileSystem.Services;

namespace PortableVersion
{
    public partial class PendingChanges : Form
    {
        public PendingChanges()
        {
            InitializeComponent();
        }

        public Action<DataTable> UpdateBack { get; set; }
        public Action<string> OpenMergeTool { get; set; }
        public void SetConflicts(DataTable conflicts)
        {
            var tbl = DataSet.Tables["Conflicts"];
            tbl.Clear();
            foreach (DataRow row in conflicts.Rows)
            {
                tbl.ImportRow(row);
            }
            tbl.AcceptChanges();
        }
        public void SetSafeChanges(DataTable changes)
        {
            var tbl = DataSet.Tables["Safe Changes"];
            tbl.Clear();
            foreach (DataRow row in changes.Rows)
            {
                tbl.ImportRow(row);
            }
            tbl.AcceptChanges();
        }

        private void GridSafeChanges_SelectionChanged(object sender, EventArgs e)
        {
            var gv = sender as DataGridView;
            if (gv==null)
                return;
            if (gv.SelectedRows.Count == 0)
                return;
            Invoke(new Action(() =>
                              Comments.Text =
                              gv.Rows[gv.SelectedRows[0].Index].Cells[gv.ColumnCount - 1].Value.ToString()));
        }

        private void ConflictActionMenu_Opening(object sender, CancelEventArgs e)
        {
            if (ReadOnly)
            {
                e.Cancel = true;
                return;
            }
            if (GridConflicts.SelectedRows.Count == 0)
            {
                MnuMerge.Enabled = false;
                return;
            }
            var rows = GridConflicts.SelectedRows.Cast<DataGridViewRow>();
            MnuOverwrite.Enabled = true;
            MnuMerge.Enabled = true;
            if (rows.Any(r => ((string)r.Cells["ConflictType"].Value) == "LocationConflict"))
            {
                MnuMerge.Enabled = false;
                MnuOverwrite.Enabled = false;
            }
            if (GridConflicts.SelectedRows.Count > 1)
            {
                MnuMerge.Enabled = false;
            }
        }

        private void MnuOverwrite_Click(object sender, EventArgs e)
        {
            SetSelectedItemsAction(ConflictAction.Overwrite);
        }

        private void SetSelectedItemsAction(ConflictAction action)
        {
            foreach (DataGridViewRow selectedRow in GridConflicts.SelectedRows)
            {
                selectedRow.Cells["Resolution"].Value = Enum.GetName(typeof (ConflictAction), action);
            }
            UpdateBack(DataSet.Tables["Conflicts"]);
        }

        private void MnuIgnore_Click(object sender, EventArgs e)
        {
            SetSelectedItemsAction(ConflictAction.Ignore);
        }

        private void MnuMerge_Click(object sender, EventArgs e)
        {
            SetSelectedItemsAction(ConflictAction.Merge);
            OpenMergeTool((String)GridConflicts.SelectedRows[0].Cells["MetaKey"].Value);
        }

        private void GridConflicts_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private bool _readOnly;
        public bool ReadOnly
        {
            get { return _readOnly; }
            set { _readOnly = value;
            if (_readOnly)
                Text = @"Successfull Check In Report";
            else
                Text = @"Pending Changes - There are conflicts to be resolved.";
            }
        }

        internal void SetErrors(DataTable errTbl)
        {
            var tbl = DataSet.Tables["Errors"];
            tbl.Clear();
            foreach (DataRow row in errTbl.Rows)
            {
                tbl.ImportRow(row);
            }
            tbl.AcceptChanges();
        }
    }
}
